package com.ykl.base.comp;

import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public abstract class AbstractJpaEntityComp<T> implements EntityComp<T> {

    private final JpaRepository<T, String> repo;

    protected AbstractJpaEntityComp(JpaRepository<T, String> repo){
        this.repo = repo;
    }

    @Override
    public T save(T entity){
        return repo.saveAndFlush(entity);
    }

    @Override
    public List<T> save(Iterable<T> entities){
        return repo.saveAllAndFlush(entities);
    }

    @Override
    public void deleteById(String id){
        repo.deleteById(id);
    }

    @Override
    public void delete(T entity){
        repo.delete(entity);
    }

    @Override
    public void delete(Iterable<T> entities){
        repo.deleteAllInBatch(entities);
    }

    @Override
    public void deleteByIds(Iterable<String> ids){
        repo.deleteAllByIdInBatch(ids);
    }

    @Override
    public Optional<T> findById(String id){
        return repo.findById(id);
    }

    @Override
    public List<T> listAll(Sort sort) {
        return repo.findAll(sort);
    }

    @Override
    public List<T> listAll() {
        return repo.findAll();
    }
}
